import { Sequelize } from "sequelize-typescript";
import path from "path";

const sequelize = new Sequelize(process.env.DB_NAME as string, process.env.DB_USER as string, process.env.DB_PASSWORD, {
    host: process.env.DB_HOST as string,
    port: process.env.DB_PORT as unknown as number,
    dialect: 'mysql',
    models: [path.join(__dirname, '..','model/**/*.ts'), path.join(__dirname, '..','model/**/*.js')],
    define: {
        timestamps: true,
        createdAt: "created_at",
        updatedAt: "updated_at",
        deletedAt: "deleted_at",
    },
});

sequelize.sync();
const db = async () => {
    try {
        await sequelize.authenticate();
        console.log('Connection has been established successfully.');
    } catch (error) {
        console.error('Unable to connect to the database:', error);
    }
}

export default db;